package com.wanmait.jd.dao;

import com.wanmait.jd.util.DBHelper;
import com.wanmait.jd.vo.Userinfo;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.management.Query;
import java.sql.SQLException;
import java.util.List;

public class UserinfoDAO {

    public List<Userinfo> findAll(){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());//连接Druid

        String sql = "SELECT *,registe_time as registerTime FROM userinfo";
        try {
            List<Userinfo> userinfoList = queryRunner.query(sql,new BeanListHandler<>(Userinfo.class));
            return userinfoList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        //return null;
    }

    public Userinfo findById(int id){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());

        String sql = "SELECT * FROM userinfo WHERE id=?";

        try {
            Userinfo userinfo = queryRunner.query(sql,new BeanHandler<>(Userinfo.class),id);
            return userinfo;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
//        return null;
    }

    public void insert(Userinfo userinfo){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());

        String sql = "INSERT INTO userinfo(username,password,userface) VALUES(?,?,?)";

        try {
            queryRunner.update(sql,userinfo.getUsername(),userinfo.getPassword(),userinfo.getUserface());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public Userinfo findByNameAndPass(Userinfo userinfo){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());

        //String sql = "SELECT * FROM userinfo WHERE username='"+userinfo.getUsername()+"'AND password= '"+userinfo.getPassword()+"'";
        String sql = "SELECT * FROM userinfo WHERE username =? AND password =?";

        try {
            Userinfo loginUser =  queryRunner.query(sql,new BeanHandler<>(Userinfo.class),userinfo.getUsername(),userinfo.getPassword());
            return loginUser;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
//        return null;
    }

    public Userinfo findByUsername(String username){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());

        String sql = "SELECT * FROM userinfo WHERE username=?";
        try {
            Userinfo userinfo = queryRunner.query(sql,new BeanHandler<>(Userinfo.class),username);
            return userinfo;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        //return null;
    }
}
//    public static void main(String[] args) {
//        UserinfoDAO userinfoDAO = new UserinfoDAO();
//        List<Userinfo> userinfoList = userinfoDAO.findAll();
//        for (Userinfo userinfo:userinfoList){
//            System.out.println(userinfo.getUsername());
//        }
//    }
